/*
 * @Author: szx
 * @Date: 2021-12-15 14:51:37
 * @LastEditTime: 2021-12-15 14:55:08
 * @Description:
 * @FilePath: \leetcode\100-199\116\116.js
 */
var connect = function (root) {
    if (!root) return null;
    const que = [];
    que.push(root);
    while (que.length) {
        const length = que.length;
        let tmp = que.shift();
        if (tmp.left) que.push(tmp.left);
        if (tmp.right) que.push(tmp.right);
        for (let i = 1; i < length; i++) {
            let tmp2 = que.shift();
            tmp.next = tmp2;
            tmp = tmp2;
            if (tmp.left) que.push(tmp.left);
            if (tmp.right) que.push(tmp.right);
        }
    }
    return root;
};
